package com.xzx.java.kit.util;

import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLDecoder;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * @version 1.0
 * @author xinzhixuan
 * @date 2022/11/21 09:27
 */
public class AESUtil {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(AESUtil.class);

    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static byte[] key = new byte[]{0x6e,0x26,0x5f,0xa,0x30,0x16,0x29,0x20,0x30,0x29,0x1c,0x52,0x42,0x1c,0x19,0x6a};
    private static byte[] iv = new byte[]{0x55,0x6,0x70,0x16,0x39,0x20,0x5a,0x29,0x66,0x7d,0x73,0x7,0x2c,0x45,0x35,0x47};

    private static String key2 = "AD382BC583B44B938FA1360096A993F7AED52DE521C2A5B1E";

    // 加密
    public static byte[] AES_cbc_encrypt(byte[] srcData) throws Exception{
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
        return cipher.doFinal(srcData);
    }

    // 解密
    public static byte[] AES_cbc_decrypt(byte[] encData) throws Exception{
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
        return cipher.doFinal(encData);
    }

    //加密
    public static String encrypt(String srcData)  {
        try {
            byte[] res = AES_cbc_encrypt(srcData.getBytes("utf-8"));
            return Base64.getEncoder().encodeToString(res);
        } catch (Exception e) {
            logger.error("AES加密异常，srcData="+srcData,e);
        }
        return "";
    }

    //解密
    public static String decrypt(String encData) throws Exception {
        byte[] data = Base64.getDecoder().decode(encData);;
        byte[] res = AES_cbc_decrypt(data);
        return new String(res,"utf-8");
    }

    public static byte[] aes_encrypt2(String content) {
        try {
            SecretKey secretKey = getKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt2(String content) {
        try {
            byte[] res = aes_encrypt2(content);
            return Base64.getEncoder().encodeToString(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] aes_decrypt2(byte[] content) {
        try {
            SecretKey secretKey = getKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(content);
            return result; // 加密
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static SecretKey getKey() throws Exception{
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
        secureRandom.setSeed(key2.getBytes());
        kgen.init(128, secureRandom);
        return kgen.generateKey();
    }

    //解密
    public static String decrypt2(String encData) throws Exception {
        byte[] data = Base64.getDecoder().decode(encData);;
        byte[] res = aes_decrypt2(data);
        return new String(res,"utf-8");
    }


    public static void main(String[] args) throws Exception{
        String nameEnc = encrypt("lisi");
        String phoneEnc = encrypt("15278416564");
//		String cardEnc = encrypt("421023199110224158");
        //System.out.println(decrypt("qKMDElZvgWEz8AiCnRWaiw=="));
        System.out.println(phoneEnc);
//		System.out.println(cardEnc);
        System.out.println(decrypt("W9NP6+Pxg7OFILzfZsKg1w=="));

//		System.out.println(decrypt2(nameEnc));
//		System.out.println(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
		/*System.out.println(decrypt(phoneEnc));
		System.out.println(decrypt(cardEnc));*/

        System.out.println(URLDecoder.decode("W9NP6+Pxg7OFILzfZsKg1w==","utf-8"));

        System.out.println("==>" + decrypt("flXugyS7f+1LwNJS12e6pQ=="));
    }
}
